package com.ruoyi.total.autoInsect.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.detection.controller.ModelController;
import com.ruoyi.topInsectImg.controller.OpenApiController;
import com.ruoyi.topInsectImg.domain.WebFrontEnd;
import com.ruoyi.topInsectImg.service.OpenApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * 功能：完成虫情测报灯图片自动下载，检测
 *
 * @author Mr-Le
 */
@Api("自动下载 insectImage,并检测")
@Slf4j
@RestController
@RequestMapping("/autoDownload")
public class AutoInsectController {

    /**
     * 存储文件的路径
     */
    @Value("${ruoyi.profile}")
    private String profile;

    private static final int TOP_EXCEPTION = 500;


    @Autowired
    private OpenApiController openApiController;

    @Autowired
    private ModelController modelController;

    @Autowired
    private OpenApiService openApiService;

    /**
     * 完成自动下载及检测，写入数据库
     *
     * @param startDate 开始时间 格式：20210401203000  年月日时分秒
     * @param endDate   结束时间 格式：20210401203000
     * @return 状态信息
     */
    @ApiOperation("完成自动下载及检测，结果写入数据库")
    @GetMapping("/down")
    public AjaxResult downAndDetection(String startDate, String endDate) {
        AjaxResult ajaxResult = openApiController.topInsectImg(startDate, endDate);
//        while ((int) ajaxResult.get("code") == TOP_EXCEPTION) {
//            ajaxResult = openApiController.topInsectImg(startDate, endDate);
//        }
//        AjaxResult ajaxResult = openApiController.topInsectImg("20210401203000", "20210401205900");
        log.info("ajaxResult:" + ajaxResult);
        /* 拿到新增的图片数量 */
        int num = (int) ajaxResult.get("data");

        List<WebFrontEnd> webFrontEnds = openApiService.selectInsectImgByNum(num);
        for (WebFrontEnd frontEnd : webFrontEnds) {
            try {
                AjaxResult model = modelController.model(file2MultipartFile(frontEnd.getUrl().replace("/profile", profile),
                        frontEnd.getImageName()));
                log.info(model.toString());
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        }
        return AjaxResult.success("下载图片、检测操作成功！");
    }


    /**
     * File 类型转 MultipartFile 类型
     *
     * @param url
     * @param name
     * @return
     */
    public static MultipartFile file2MultipartFile(String url, String name) {
        MultipartFile multipartFile = null;
        File file = new File(url);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
            return multipartFile;
    }
}

